package com.laijava.demo;

import java.util.ArrayList;
import java.util.List;
/**
 *
 * @Author Seven
 * @Description  指定长度截取字符串
 * @Date 14:42 2019/10/15
 **/
public class SubStringByBytes {
    public static void main(String[] args ){
    String str = "<notes><note><note_timestamp>20181127163357</note_timestamp><note_seq>1</note_seq><business_type>tcm_makeup</business_type><note_id>201811271606365339</note_id><note_invoicenum>+1001088276</note_invoicenum><note_diagnose>西医诊断：精神障碍 中医病名： 中医证型：阴虚痰热</note_diagnose><is_concerted>0</is_concerted><send_flag>unsent</send_flag><note_datetime>2018-11-27 16:06:36</note_datetime><note_name></note_name><note_cat></note_cat><note_cat2></note_cat2><note_cat3>0</note_cat3><note_cat4></note_cat4><note_cat5></note_cat5><repetition>7</repetition><deliver_time></deliver_time><med_startdate></med_startdate><med_enddate></med_enddate><is_heavy>0</is_heavy><is_outside>0</is_outside><is_child>0</is_child><note_flag>common</note_flag><dtr_advice>【4号窗】</dtr_advice><dtr_name>叶沐镕</dtr_name><pat_num>9828757736</pat_num><pat_name>李太和</pat_name><insurance_num></insurance_num><pat_gender>M</pat_gender><pat_age>28岁</pat_age><receipt_mode>express</receipt_mode><dept_name>治未病中心</dept_name><bed_num></bed_num><receipt>李太和</receipt><mobilephone>18933365384</mobilephone><postcode>528459</postcode><addr_province>广东省</addr_province><addr_city>中山市</addr_city><addr_area>板芙镇</addr_area><addr_detail>里溪大道29号</addr_detail><source_mode>hospital</source_mode><is_doublefried>0</is_doublefried><is_verified>1</is_verified><verifier>何鉴洪</verifier><verification_time>2018-11-27 16:33:57</verification_time><is_pregnant>0</is_pregnant><note_items><note_item><note_id>201811271606365339</note_id><item_id>1</item_id><material_id>51101</material_id><material_name>蒸陈皮</material_name><material_spec>1克</material_spec><dosage>5</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.035</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>2</item_id><material_id>50074</material_id><material_name>法半夏</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.231</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>3</item_id><material_id>54201</material_id><material_name>茯苓</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.055</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>4</item_id><material_id>50011</material_id><material_name>甘草片</material_name><material_spec>1克</material_spec><dosage>5</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.061</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>5</item_id><material_id>51089</material_id><material_name>枳实(蒸)</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.089</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>6</item_id><material_id>54109</material_id><material_name>竹茹</material_name><material_spec>1克</material_spec><dosage>15</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.041</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>7</item_id><material_id>50021</material_id><material_name>石菖蒲</material_name><material_spec>1克</material_spec><dosage>20</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.104</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>8</item_id><material_id>50076</material_id><material_name>胆南星</material_name><material_spec>1克</material_spec><dosage>15</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.091</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>9</item_id><material_id>50082</material_id><material_name>防己</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.194</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>10</item_id><material_id>50063</material_id><material_name>生地黄</material_name><material_spec>1克</material_spec><dosage>30</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.049</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>11</item_id><material_id>54101</material_id><material_name>桂枝</material_name><material_spec>1克</material_spec><dosage>5</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.038</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>12</item_id><material_id>50053</material_id><material_name>白术</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.081</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>13</item_id><material_id>50083</material_id><material_name>防风</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.464</material_price></note_item></note_items></note></notes>\n" +
            "<notes><note><note_timestamp>20181127163357</note_timestamp><note_seq>1</note_seq><business_type>tcm_makeup</business_type><note_id>201811271606365339</note_id><note_invoicenum>+1001088276</note_invoicenum><note_diagnose>西医诊断：精神障碍 中医病名： 中医证型：阴虚痰热</note_diagnose><is_concerted>0</is_concerted><send_flag>unsent</send_flag><note_datetime>2018-11-27 16:06:36</note_datetime><note_name></note_name><note_cat></note_cat><note_cat2></note_cat2><note_cat3>0</note_cat3><note_cat4></note_cat4><note_cat5></note_cat5><repetition>7</repetition><deliver_time></deliver_time><med_startdate></med_startdate><med_enddate></med_enddate><is_heavy>0</is_heavy><is_outside>0</is_outside><is_child>0</is_child><note_flag>common</note_flag><dtr_advice>【4号窗】</dtr_advice><dtr_name>叶沐镕</dtr_name><pat_num>9828757736</pat_num><pat_name>李太和</pat_name><insurance_num></insurance_num><pat_gender>M</pat_gender><pat_age>28岁</pat_age><receipt_mode>express</receipt_mode><dept_name>治未病中心</dept_name><bed_num></bed_num><receipt>李太和</receipt><mobilephone>18933365384</mobilephone><postcode>528459</postcode><addr_province>广东省</addr_province><addr_city>中山市</addr_city><addr_area>板芙镇</addr_area><addr_detail>里溪大道29号</addr_detail><source_mode>hospital</source_mode><is_doublefried>0</is_doublefried><is_verified>1</is_verified><verifier>何鉴洪</verifier><verification_time>2018-11-27 16:33:57</verification_time><is_pregnant>0</is_pregnant><note_items><note_item><note_id>201811271606365339</note_id><item_id>1</item_id><material_id>51101</material_id><material_name>蒸陈皮</material_name><material_spec>1克</material_spec><dosage>5</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.035</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>2</item_id><material_id>50074</material_id><material_name>法半夏</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.231</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>3</item_id><material_id>54201</material_id><material_name>茯苓</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.055</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>4</item_id><material_id>50011</material_id><material_name>甘草片</material_name><material_spec>1克</material_spec><dosage>5</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.061</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>5</item_id><material_id>51089</material_id><material_name>枳实(蒸)</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.089</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>6</item_id><material_id>54109</material_id><material_name>竹茹</material_name><material_spec>1克</material_spec><dosage>15</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.041</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>7</item_id><material_id>50021</material_id><material_name>石菖蒲</material_name><material_spec>1克</material_spec><dosage>20</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.104</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>8</item_id><material_id>50076</material_id><material_name>胆南星</material_name><material_spec>1克</material_spec><dosage>15</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.091</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>9</item_id><material_id>50082</material_id><material_name>防己</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.194</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>10</item_id><material_id>50063</material_id><material_name>生地黄</material_name><material_spec>1克</material_spec><dosage>30</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.049</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>11</item_id><material_id>54101</material_id><material_name>桂枝</material_name><material_spec>1克</material_spec><dosage>5</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.038</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>12</item_id><material_id>50053</material_id><material_name>白术</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.081</material_price></note_item><note_item><note_id>201811271606365339</note_id><item_id>13</item_id><material_id>50083</material_id><material_name>防风</material_name><material_spec>1克</material_spec><dosage>10</dosage><dosage_unit>克</dosage_unit><item_advice></item_advice><material_price>0.464</material_price></note_item></note_items></note></notes>";
      List<String> a =   subStringByBytes(str, 400);
    }

    private static List<String> subStringByBytes(String str, int len) {
        List<String> resultString = new ArrayList<String>();
        if (str == null || str.length() == 0) {
            return resultString;
        }
        int totalLen = 0;
        int startIndex = 0;
        for (int i=0; i<str.length(); i++ ) {
            //GBK 编码格式 中文占两个字节 UTF-8 编码格式中文占三个字节;
            totalLen += (str.charAt(i)>255 ? 3 : 1);
            if(totalLen>len){
                resultString.add(str.substring(startIndex,i));
                startIndex = i ;
                totalLen=0;
            }
        }
        return resultString;
    }
}
